//
//  PromptView.m
//  MobileOffice
//
//  Created by jjkj on 16/11/2.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "PromptView.h"

@implementation PromptView


- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title message:(NSString *)message prompt:(NSString *)prompt isHidePrompt:(BOOL)isHide submitTitle:(NSString *)submitTitle{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor=[UIColor clearColor];
        
        UIView *backView=[[UIView alloc]initWithFrame:frame];
        backView.backgroundColor=[UIColor blackColor];
        backView.alpha=0.4;
        [self addSubview:backView];
        
        UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(50, 200, IPHONE_WIDTH-2*50, 150)];
        contentView.backgroundColor=[UIColor whiteColor];
        contentView.layer.masksToBounds=YES;
        contentView.layer.cornerRadius=8;
        [self addSubview:contentView];
        
        _myTitleLabel=[MyControl creatLabelWithFrame:CGRectMake(8, 8, contentView.width-16, 20) text:title];
        _myTitleLabel.textAlignment=NSTextAlignmentCenter;
        _myTitleLabel.font=FONT_16;
        [contentView addSubview:_myTitleLabel];
        if (title==nil||title.length==0) {
            _myTitleLabel.height=0;
        }
        
        //内容信息
        _messageLabel=[MyControl creatLabelWithFrame:CGRectMake(8, _myTitleLabel.y+_myTitleLabel.height+8, contentView.width-16, 20) text:message];
        _messageLabel.textColor=[UIColor blackColor];
        _messageLabel.numberOfLines=0;
        _messageLabel.font=FONT_15;
        [contentView addSubview:_messageLabel];
        CGFloat messageHeight=[LZXHelper textHeightFromTextString:message width:_messageLabel.width fontSize:15]+3;
        [_messageLabel changeView_Height:messageHeight];
        
        if (_messageLabel.y+_messageLabel.height<76) {
            _messageLabel.height=76-_messageLabel.y;
        }
        
        if (!isHide) {
            //勾选框
            _selectButton=[MyControl creatButtonWithFrame:CGRectMake(60, _messageLabel.y+_messageLabel.height, 18, 18) target:self sel:@selector(selectButtonClick:) tag:100 image:@"icon_notselect@2x" title:nil];
            [_selectButton setImage:[UIImage imageNamed:@"icon_select@2x"] forState:UIControlStateSelected];
//            _selectButton.layer.borderColor=[[UIColor blackColor]CGColor];
//            _selectButton.layer.borderWidth=2;
//            _selectButton.layer.masksToBounds=YES;
//            _selectButton.layer.cornerRadius=2;
            [contentView addSubview:_selectButton];
            
            //提示语
            _promptLabel=[MyControl creatLabelWithFrame:CGRectMake(_selectButton.x+_selectButton.width+15, _selectButton.y-1, contentView.width-(_selectButton.x+_selectButton.width+10), 20) text:prompt];
            _promptLabel.font=[UIFont boldSystemFontOfSize:15];
            [contentView addSubview:_promptLabel];
            
            //确定按钮
            _submitButton=[MyControl creatButtonWithFrame:CGRectMake((contentView.width-120)/2,_selectButton.y+_selectButton.height+8, 120, 40) target:self sel:@selector(submitClick:) tag:101 image:nil title:submitTitle];
        }else{
            //确定按钮
            _submitButton=[MyControl creatButtonWithFrame:CGRectMake((contentView.width-120)/2,_messageLabel.y+_messageLabel.height+8, 120, 40) target:self sel:@selector(submitClick:) tag:101 image:nil title:submitTitle];
        }
        

        _submitButton.layer.masksToBounds=YES;
        _submitButton.layer.cornerRadius=5;
        [_submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _submitButton.backgroundColor=COLOR_MIDBLUE;
        [contentView addSubview:_submitButton];
        [contentView changeView_Height:_submitButton.y+_submitButton.height+8];
        [contentView changeView_Y:(IPHONE_HEIGHT-(_submitButton.y+_submitButton.height+8))/2];
//        _lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 49, IPHONE_WIDTH, 1)];
//        _lineLabel.backgroundColor=BACKCOLOR_GRAY;
//        [self.contentView addSubview:_lineLabel];
    }
    return self;
}

- (void)selectButtonClick:(UIButton *)button{
    button.selected=!button.selected;
}

- (void)submitClick:(UIButton *)button{
    if (_selectButton) {
        if (_selectButton.selected) {
            [[NSUserDefaults standardUserDefaults]setObject:[NSNumber numberWithBool:NO] forKey:@"switch1"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        }
    }
    [self removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
